from pyglet.gl import *
import data
import pyglet
import medals

pause_label = pyglet.text.HTMLLabel('<b>PAUSED</b>', x=365, y = 320)
pause_msg = pyglet.text.HTMLLabel('<font color=#ffffff><b>Press Esc to Quit or Click to Continue</b></font>', x=266, y=200)
label = pyglet.text.HTMLLabel('<b>Click to Start</b>', x=300, y =100)
title_img = pyglet.image.load(data.filepath('title.png'))
time_var = 0
color = '#000000'
medals = medals.Medals()
medals_list = []
sy = 500-title_img.height - 50
sx  = 400-title_img.width/2
dt = 20
i = 0
a_label = pyglet.text.HTMLLabel('<u><b>ACHEIVEMENTS:</b></u>', x=sx, y=sy)
for s in medals.parser.sections():
    if s == 'HighScore':
        continue
    i += 1
    if medals.parser.getboolean(s,'done'):
        color = "#000000"
    else:
        color = "#cccccc"
    l = pyglet.text.HTMLLabel('<font color=%s><b> %s:%s </b></font>' % (color, s, medals.parser.get(s,'task')), x=sx, y =(sy -10 - i * dt))
    medals_list.append(l)
pop_label = pyglet.text.HTMLLabel('start', x=800,y=100)
pop_pos = (800, 50)
pop_done = False
pop_w = 150
pop_show = (800-pop_w,50)
def resetPop():
    global pop_pos, pop_done
    pop_pos = (800, 50)
    pop_done = False

def updatePopUp():
    global pop_pos, pop_done
    global pop_show
    global time_var
    global pop_w
    dt = -2
    if pop_pos == pop_show:
        if not pop_done:
            pop_done = True
            time_var = 0
    if not pop_done:
        pop_pos = (pop_pos[0]+dt, pop_pos[1])
    else:
        time_var = time_var + 1

def drawRectImage(pos, w, h, col=(1, 1, 1, 1)):
    glColor4fv((GLfloat*4)(*col))
    glBindTexture(GL_TEXTURE_2D, data.rect_image.id)
    glBegin(GL_POLYGON)
    glTexCoord2f(0, 0)
    glVertex2f(pos[0], pos[1])
    glTexCoord2f(0, 1)
    glVertex2f(pos[0], pos[1]-h)
    glTexCoord2f(1, 1)
    glVertex2f(pos[0]+w, pos[1]-h)
    glTexCoord2f(1, 0)
    glVertex2f(pos[0]+w, pos[1])
    glEnd()

def drawPopUp(msg):
    global time_var
    global pop_label, pop_done, pop_pos
    global pop_w
    pop_h = 50
    pop_label.text = '<i>' + msg + '</i>'
    drawRectImage(pop_pos, pop_w, pop_h)
    pop_label.x = pop_pos[0] + 50
    pop_label.y = pop_pos[1] - 30
    pop_label.draw()

def drawPauseMenu():
    global pause_label, pause_msg
    glDisable(GL_TEXTURE_2D)
    drawRectImage((0, 600), 800, 600, (0, 0, 0, 0.5))
    glEnable(GL_TEXTURE_2D)
    drawRectImage((300,350), 200, 50)
    pause_label.draw()
    pause_msg.draw()

def drawMenu():
    global time_var, color, label, medals_list
    global a_label
    glClearColor(1, 1, 1, 1)
    glClear(GL_COLOR_BUFFER_BIT)
    sprite = pyglet.sprite.Sprite(title_img)
    sprite.x = 400-title_img.width/2
    sprite.y = 500-title_img.height
    sprite.draw()

    for l in medals_list:
        l.draw()
    a_label.draw()
    if time_var % 20 == 0:
        time_var = 0
        if color == '#000000':
            color = '#ffffff'
        else:
            color = '#000000'


    label.text = '<h3><font color='+color+'>Click key to start</font></h3>'

    label.draw()
    time_var +=1
